package com.android.lyricshow.until;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.android.lyricshow.R;
import com.android.lyricshow.bean.SongBean;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class AppUntil {
	
	public static final int SIZE_SMALL	= 4;
	public static final int SIZE_NORMAL	= 2;
	public static final int SIZE_LARGER	= 1;

	public static final String ROOT_PATH = Environment.getExternalStorageDirectory().getPath() + "/cindy/";
	
	public static void showToast(Context context, int resId){
		Toast.makeText(context, resId, Toast.LENGTH_SHORT).show();
	}
	
	public static void showToast(Context context, String message){
		Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}
	
	public static String searchLRCFromPath(SongBean sbean){

		String path = searchLRCFromPath(sbean.getTitle(), ROOT_PATH);
		if(!"".equals(path)){
			return path;
		}
		
		String songpath = sbean.getPath();
		songpath = songpath.substring(songpath.lastIndexOf("/"));
		
		return searchLRCFromPath(sbean.getTitle(), songpath);
	}

	private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
	private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
	
	public static Bitmap getBitmapFromDB(Context context, long aid, int SampleSize, int resId){
		Bitmap bm = null;
		
		bm = getBitmapFromDB(context, aid, SampleSize);
		
		if(bm == null ){
			bm = BitmapFactory.decodeResource(context.getResources(), resId);
		}
		
		return bm;
	}
	public static Bitmap getBitmapFromDB(Context context, long aid, int SampleSize){
		
		Bitmap bm = null;
		
		if(aid > 0)
		{
			ContentResolver res = context.getContentResolver();
			Uri uri = ContentUris.withAppendedId(sArtworkUri, aid);
			if (uri != null) 
			{
	            InputStream in = null;
	            try 
	            { 
	                in = res.openInputStream(uri);
	                sBitmapOptions.inSampleSize = SampleSize;
	                bm = BitmapFactory.decodeStream(in, null, sBitmapOptions);
	            } 
	            catch (FileNotFoundException e) 
	            {} 
	            finally 
	            {
	                try 
	                {
	                    if (in != null) 
	                    {
	                        in.close();
	                    }
	                } 
	                catch (IOException e) 
	                {
	                }
	            }
	        }
		}
		
		if(bm != null && SampleSize == SIZE_SMALL){
			return getRoundCornerBitmap(bm);
		}
        return bm;
	}
	
	public static Bitmap getRoundCornerBitmap(Bitmap bmp){
		
		int width 	= bmp.getWidth();
		int height 	= bmp.getHeight();
		int corner	= width/10;
		
		Bitmap bmpR = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(bmpR);
		
		Paint paint	= new Paint();
		Rect rect = new Rect(0, 0, width, height);
		RectF rf = new RectF(rect);
		
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawRoundRect(rf, corner, corner, paint);
		
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bmp, rect, rect, paint);
		
		return bmpR;
	}
	
	public static String searchLRCFromPath(String name, String path){
		
		String lrcpath = "";
		
		File[] files = new File(path).listFiles();
		if(files == null){
			return lrcpath;
		}
		
		for(File file : files){
			
			String filename = file.getName();
			
			int pos = filename.indexOf(name);
			if(pos != -1 
				&& (filename.endsWith(".txt") 
				|| filename.endsWith(".lrc"))){
				
				lrcpath = file.getPath();
				break;
			}
		}
		
		return lrcpath;
	}
	
	public static long toMinute(long time){
		
		time /= 1000;
		
		long minute = time / 60;
		long second = time % 60;
		
		minute %= 60;
		
		return second > 0 ? minute + 1 : minute;
	}
	
	public static String toTime(long time) {
		
		time /= 1000;
		
		long minute = time / 60;
		long second = time % 60;
		
		minute %= 60;
		return String.format("%02d:%02d", minute, second);
	}
	
	public static void flipperShowPrevious(Context context, ViewFlipper vf){
		
		vf.setInAnimation(context, R.anim.push_left_in);
		vf.setOutAnimation(context, R.anim.push_right_out);
		vf.showPrevious();
	}
	
	public static void flipperShowNext(Context context, ViewFlipper vf){
		
		vf.setInAnimation(context, R.anim.push_right_in);
		vf.setOutAnimation(context, R.anim.push_left_out);
		vf.showNext();
	}
	
	public static void showView(View view){
		AnimationController animationController = new AnimationController();
		animationController.fadeIn(view, 500, 0);
	}
	
	public static void GoneView(View view){
		AnimationController animationController = new AnimationController();
		animationController.fadeOut(view, 500, 0);
	}
	
	public static int getRate(int max, int now){
		return (now * 100)/max;
	}
	
    public static boolean hasFroyo() {
        // Can use static final constants like FROYO, declared in later versions
        // of the OS since they are inlined at compile time. This is guaranteed behavior.
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;
    }

    public static boolean hasGingerbread() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;
    }

    public static boolean hasHoneycomb() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
    }

    public static boolean hasHoneycombMR1() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
    }

    public static boolean hasJellyBean() {
    	return false;
        //return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
    }
}
